﻿using System;
using System.Threading.Tasks;
using System.Web.Http;
using Autofac;
using Pay.Core;
using Pay.DTO;
using Pay.DTO.chpay;
using Pay.IService;
using Pay.Service;
using Pay.TO;

namespace Pay.Api.Controllers {
	/// <summary>
	/// 接口
	/// </summary>
	public partial class OpenController : BaseController {
		/// <summary>
		/// 提现
		/// </summary>
		/// <returns>The receive.</returns>
		/// <param name="model">Model.</param>
		protected async Task<transfer_result> Transfer(apply_transfer model) {
			model.pay_channel = PayChannel.ChPay;
			model.pay_code = "PAYOFF";
			if (string.IsNullOrEmpty(model.bind_no))
				return ApiResultToJson.Json<transfer_result>("提现商户号不能为空");
			if (string.IsNullOrEmpty(model.order_no))
				return ApiResultToJson.Json<transfer_result>("订单号不能为空");
			else {
				if (model.order_no.Length < 13)
					return ApiResultToJson.Json<transfer_result>("订单号最小14位");
				if (model.order_no.Length > 31)
					return ApiResultToJson.Json<transfer_result>("订单号最大长度30位");
			}
			//验证请求数据
			decimal amount = decimal.Parse(model.tran_amount) / 100;
			if (string.IsNullOrEmpty(model.notify))
				return ApiResultToJson.Json<transfer_result>("结算后服务器回调该地址不能为空");
			if (model.pay_channel == PayChannel.None)
				return ApiResultToJson.Json<transfer_result>("通道不能为空");

			//验证额度够不够,API上验证
			//if (model.settle_type == "T0") {
			//	var quota = await _iPayService.Quota("1");
			//	if (quota.data.balance < decimal.Parse(model.tran_amount) / 100)
			//		return ApiResultToJson.Json<transfer_result>("TO额度上限");
			//}
			var _iPayService = _iocContext.ResolveKeyed<IPayService>(model.pay_channel);
			return await _iPayService.Transfer(model);
		}

		/// <summary>
		/// 批量代付
		/// </summary>
		/// <returns>The transfer.</returns>
		/// <param name="model">Reqt.</param>
		//[Route("batch_transfer"), HttpPost]
		//public async Task<ResultMessage<batch_transfer_result>> BatchTransfer([FromBody]batch_transfer model) {
		//	if (model.orders.Count == 0)
		//		return ApiResultToJson.Json<batch_transfer_result>("没有请求的提现单");

		//	var _iPayService = _iocContext.ResolveKeyed<IPayService>(model.pay_channel);
		//	return await _iPayService.Transfer(model);
		//}

		/// <summary>
		/// 提现查询
		/// </summary>
		/// <returns>The pay query.</returns>
		/// <param name="model">Model.</param>
		protected async Task<transfer_result> TransferQuery(transfer_query model) {
			if (string.IsNullOrEmpty(model.order_no) && string.IsNullOrEmpty(model.transaction_no))
				return ApiResultToJson.Json<transfer_result>("查询订单号或流水号不正确");
			if (model.pay_channel == PayChannel.None)
				return ApiResultToJson.Json<transfer_result>("通道不能为空");

			var _iPayService = _iocContext.ResolveKeyed<IPayService>(model.pay_channel);
			return await _iPayService.TransferQuery(model);
		}

		/// <summary>
		/// 代收签约－会发短信
		/// </summary>
		/// <returns>The receive.</returns>
		/// <param name="model">Model.</param>
		protected async Task<ApiResultMessage> InsteadReceive(verify_model model) {
			if (string.IsNullOrEmpty(model.account_no))
				return new ApiResultMessage(0, "账户不能为空");
			if (string.IsNullOrEmpty(model.account_name))
				return new ApiResultMessage(0, "姓名不能为空");
			if (string.IsNullOrEmpty(model.bank_name))
				return new ApiResultMessage(0, "银行名称不能为空");
			if (string.IsNullOrEmpty(model.id_card))
				return new ApiResultMessage(0, "身份证不能为空");
			if (string.IsNullOrEmpty(model.mobile))
				return new ApiResultMessage(0, "预留手机号码");

			var _iPayService = _iocContext.ResolveKeyed<IPayService>(PayChannel.HF);
			return await _iPayService.InsteadReceive(model);
		}

		/// <summary>
		/// 代收签约－提交短信验证码
		/// </summary>
		/// <returns>The receive.</returns>
		/// <param name="model">Model.</param>
		protected async Task<ApiResultMessage> InsteadReceive(verify_model_sms model) {
			if (string.IsNullOrEmpty(model.orig_tran_id))
				return new ApiResultMessage(0, "银行卡相关信息不完善");
			if (string.IsNullOrEmpty(model.code))
				return new ApiResultMessage(0, "验证码不能为空");

			var _iPayService = _iocContext.ResolveKeyed<IPayService>(PayChannel.HF);
			return await _iPayService.InsteadReceive(model);
		}

		/// <summary>
		/// 单笔代收
		/// </summary>
		/// <returns>The fund.</returns>
		/// <param name="model">Model.</param>
		protected async Task<transfer_result> AgencyFund(apply_receive model) {
			if (string.IsNullOrEmpty(model.order_no))
				return ApiResultToJson.Json<transfer_result>("订单号不能为空");
			else {
				if (model.order_no.Length < 13)
					return ApiResultToJson.Json<transfer_result>("订单号最小14位");
				if (model.order_no.Length > 31)
					return ApiResultToJson.Json<transfer_result>("订单号最大长度30位");
			}
			if (string.IsNullOrEmpty(model.tran_amount))
				return ApiResultToJson.Json<transfer_result>("交易金额不能为空");
			if (string.IsNullOrEmpty(model.account_no))
				return ApiResultToJson.Json<transfer_result>("支付方银行卡号不能为空");
			if (string.IsNullOrEmpty(model.rcv_bank_name))
				return ApiResultToJson.Json<transfer_result>("收款方姓名不能为空");
			if (string.IsNullOrEmpty(model.rcv_account_no))
				return ApiResultToJson.Json<transfer_result>("收款方卡号不能为空");
			if (string.IsNullOrEmpty(model.rcv_account_name))
				return ApiResultToJson.Json<transfer_result>("收款方姓名不能为空");
			if (string.IsNullOrEmpty(model.rcv_id_card))
				return ApiResultToJson.Json<transfer_result>("收款方身份证号不能为空");
			if (string.IsNullOrEmpty(model.notify))
				return ApiResultToJson.Json<transfer_result>("支付成功后服务器回调该地址不能为空");
			if (model.pay_channel == PayChannel.None)
				return ApiResultToJson.Json<transfer_result>("通道不能为空");

			var _iPayService = _iocContext.ResolveKeyed<IPayService>(model.pay_channel);
			return await _iPayService.AgencyFund(model);
		}

		/// <summary>
		/// OEM商户的提现
		/// </summary>
		/// <returns>The transfer query.</returns>
		/// <param name="model">Model.</param>
		protected ApiPagedResult<transfer_dto> OemTransferQuery(transfer_ex_query model) {
			var dto = new TransferQuery {
				order_no = model.order_no,
				merchant_no = model.merchant_no,
				pay_channel = model.pay_channel,
				status = model.status,
				end_time = model.end_time,
				start_time = model.start_time,
				page = model.page,
				size = model.size
			};
			var list = _iTransferService.Get(dto);
			var result = new ApiPagedResult<transfer_dto>(list.rows, list.total_result, list.page, list.size);
			return ApiResultToJson.Json(result);
		}
	}
}
